Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The openai npm package is a Node.js client library for accessing the OpenAI API, which provides access to powerful AI models such as GPT-3 for natural language processing tasks, including text generation, translation, summarization, and more. The package allows developers to easily integrate OpenAI's AI capabilities into their Node.js applications.
Text Completion
Generates text completions for a given prompt using the GPT-3 model.
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
openai.createCompletion({
model: 'text-davinci-003',
prompt: 'Translate the following English text to French: Hello, how are you?',
max_tokens: 60
}).then(response => {
console.log(response.data.choices[0].text);
});
Text Classification
Classifies a piece of text into one of the specified categories.
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
openai.createClassification({
model: 'text-davinci-003',
examples: [
['A movie about space wars and intergalactic politics', 'Science Fiction'],
['A film focusing on the love life of a New York City woman', 'Romance']
],
query: 'A story about a boy who learns he is a wizard and attends a magical school',
labels: ['Science Fiction', 'Romance', 'Fantasy']
}).then(response => {
console.log(response.data);
});
Text Summarization
Summarizes a longer piece of text into a concise version.
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
openai.createCompletion({
model: 'text-davinci-003',
prompt: 'Summarize the following text: ...',
max_tokens: 60,
temperature: 0.7
}).then(response => {
console.log(response.data.choices[0].text);
});
This package provides access to IBM Watson's AI services, which include natural language processing, speech to text, text to speech, and language translation. It is similar to openai in providing AI-powered language services, but it uses IBM's Watson AI instead of OpenAI's models.
The Google Cloud npm package allows developers to interact with Google Cloud services, including its AI and machine learning services like the Natural Language API and the Translation API. It offers functionalities similar to openai but is integrated with Google's cloud ecosystem.
This package is part of Microsoft's Azure Cognitive Services and provides capabilities for speech recognition, text-to-speech, and speech translation. It offers different services compared to openai, focusing more on speech technologies rather than text-based AI models.
The OpenAI Node.js library provides convenient access to the OpenAI API from Node.js applications. Most of the code in this library is generated from our OpenAPI specification.
⚠️ Important note: this library is meant for server-side usage only, as using it in client-side browser code will expose your secret API key. See here for more details.
npm install openai
The library needs to be configured with your account's secret key, which is available in your OpenAI account page. We recommend setting it as an environment variable. Here's an example of initializing the library with the API key loaded from an environment variable and creating a completion:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const chatCompletion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: "Hello world"}],
});
console.log(chatCompletion.data.choices[0].message);
Check out the full API documentation for examples of all the available functions.
All of the available API request functions additionally contain an optional final parameter where you can pass custom axios request options, for example:
const completion = await openai.createCompletion(
{
model: "text-davinci-003",
prompt: "Hello world",
},
{
timeout: 1000,
headers: {
"Example-Header": "example",
},
}
);
API requests can potentially return errors due to invalid inputs or other issues. These errors can be handled with a try...catch
statement, and the error details can be found in either error.response
or error.message
:
try {
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: "Hello world",
});
console.log(completion.data.choices[0].text);
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
}
Streaming completions (stream=true
) are not natively supported in this package yet, but a workaround exists if needed.
All breaking changes for major version releases are listed below.
createCompletion(engineId, params)
changed to createCompletion(params)
. The value previously passed in as the engineId
argument should now be passed in as model
in the params object (e.g. createCompletion({ model: "text-davinci-003", ... })
)createCompletionFromModel(params)
calls with createCompletion(params)
Thank you to ceifa for creating and maintaining the original unofficial openai
npm package before we released this official library! ceifa's original package has been renamed to gpt-x.
FAQs
The official TypeScript library for the OpenAI API
We found that openai demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.